package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Motor;
import negModelExcep.*;

public class MotorTest extends TestCase {
	
	Motor mot1,mot2;
	Element e;

	protected void setUp() throws Exception {
		super.setUp();
		mot1 = new Motor (1000,140,500,new String("Mot1"),new AlgoPesos(100),false,100,1000);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","motor");
		
		e.setAttribute("maxVU", ""+ mot1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ mot1.getVidaUtil());
		e.setAttribute("peso", ""+ mot1.getPeso());
		e.setAttribute("aportevmax", ""+ mot1.aportevmax());
		e.setAttribute("detalle", mot1.getDetalle());
		e.setAttribute("precio", ""+(mot1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+mot1.esRemovible());
		e.setAttribute("performance", ""+mot1.getPerformance());
		
		e.setAttribute("maxrpm", ""+mot1.getRpmMax());
		e.setAttribute("rpm", ""+mot1.getRpm());
		
	}

	public void testGastar() {
		try {
			mot1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(999,mot1.getVidaUtil(),0.0);
	}

	public void testFuncionar() {
		try {
			mot1.aumentarRpm();
			assertEquals(100,mot1.funcionar(),0.0);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion eParteNofunciona");
		}
	}

	public void testAumentarRpm(){
		
		int i = 0;
		while (i < 20){
			mot1.aumentarRpm();
			i ++;
		}
		assertEquals(1000,mot1.getRpm(),0);	
				
	}
	
	public void testDisminuirRpm(){
	
		int i = 0;
		while (i < 20){
			mot1.aumentarRpm();
			i +=1;
		}
		assertEquals(1000,mot1.getRpm(),0);	
		i = 0;
		while (i < 20){
			mot1.disminuirRpm();
			i +=1;
		}
		assertEquals(400,mot1.getRpm(),0);	
		
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=mot1.toXml(doc);
		
		assertEquals(mot1.getRpmMax(),Integer.parseInt(e.getAttribute("maxrpm")));
		assertEquals(mot1.getRpm(),Integer.parseInt(e.getAttribute("rpm")));
		
		assertEquals(mot1.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((mot1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(mot1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(mot1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(mot1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(mot1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testMotor()throws Exception {
		
		mot2=new Motor(e);
				
		assertEquals(mot2.getRpmMax(),Integer.parseInt(e.getAttribute("maxrpm")));
		assertEquals(mot2.getRpm(),Integer.parseInt(e.getAttribute("rpm")));
		
		assertEquals(mot2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((mot2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(mot2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(mot2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(mot2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(mot2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
}
